<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #app {
            padding: 10px;
            background-color: #fff;
        }

        .input-box {
            margin: 15px;
            display: flex;
            align-items: center;
        }

        .input-box input {
            flex: 1;
            height: 40px;
            border: 1px solid grey;
            outline: none;
            padding-left: 10px;
            color: red;
        }

        .input-box span {
            text-align: center;
            color: blue;
            width: 50px;
        }
        .list{
            margin-top: 10px;
        }
        .list .task{
            margin: 20px 0;
            display: flex;
            justify-content: space-around;
            padding: 10px;
            background-color: #20b2aa;
            color: #fff;
        }
        .list .task.finished{
            background-color: red;
        }
        .empty{
            text-align: center;
            margin: 40px 0;
            color: red;
        }
        .listData{
            display: flex;
            justify-content: space-around;
        }
        .all{
            background-color: #20b2aa;
            color: #fff;
            line-height: 40px;
            width: 30%;
            text-align: center;
            border-radius: 10px;
        }
        .finish{
            background-color: #20b2aa;
            color: #fff;
            width: 30%;
            line-height: 40px;
            text-align: center;
            border-radius: 10px;
        }
        .unfinish{
            background-color: red;
            color: #fff;
            width: 30%;
            line-height: 40px;
            text-align: center;
            border-radius: 10px;
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="input-box">
            <input type="text" placeholder="输入任务名称" v-model="inputText">
            <span @click="add">新增</span>
        </div>
        <div class="listData">
            <div class="all">全部任务（{{this.tasklist.length}}）</div>
            <div class="finish">已完成({{finish()}})</div>
            <div class="unfinish">未完成({{unfinish()}})</span>
            </div>
        </div>
        <h2>任务列表</h2>
        <div class="list">
            <div :class="{task: true,finished:item.status}" v-for="(item,index) in tasklist" :key="index">
                <input type="checkbox" @click="changeStatus(item)">
                <div class="number">{{item.number}}</div>
                <div class="name">{{item.name}}</div>
                <span class="delet" @click="delet(index)">删除</span>
            </div>
            <div class="empty" v-show="tasklist.length == 0">任务列表为空！</div>
        </div>
    </div>
</body>
</html>
<script src="./vue-2.5.21.js"></script>
<script>
    var v = new Vue({
        el: '#app',
        data: {
            inputText: '',//保存输入框中的值
            tasklist: [

            ]
        },
        methods: {
            add() {//添加任务
                if (this.inputText) {
                    var obj = { number:this.tasklist.length+1,id: this.tasklist.length + 1, name: this.inputText, status: false };
                    // 将任务对象添加到人物列表中
                    this.tasklist.push(obj)
                    // 清空购物车
                    this.inputText = '';
                }
            },
            changeStatus(item) {
                // 更改任务状态，item参数就是当前点击任务对象
                item.status = !item.status;
            },
            delet(item,index){
                this.tasklist.splice(index,1)
            },
            finish(){
                var count = 0
                this.tasklist.forEach((item,index)=>{
                    if(item.status == true){
                        count++
                    }
                })
                return count
            },
            unfinish(){
                var count = 0
                this.tasklist.forEach((item,index)=>{
                    if(item.status == false){
                        count++
                    }
                })
                return count
            },
        }
    })
</script>